package com.example.mediaplayer_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    ListView listView;
    Button btn_start,btn_pause,btn_previous,btn_next;
    List<MusicInfo> musicInfos =new ArrayList<>();
    TextView tvProgress;
    SeekBar skMusic;
    MediaPlayer mediaPlayer;
    Handler handler = new Handler();
    int currentMusic = 0;
    int[] musicId = {R.raw.arms,R.raw.artists,R.raw.brotherhood,R.raw.danny,R.raw.dream};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
        initEvent();
        MusicAdapter musicAdapter = new MusicAdapter(this,R.layout.music_item,musicInfos);
        listView.setAdapter(musicAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                 MusicInfo musicInfo = musicInfos.get(i);
                 int musicId= musicInfo.getId();
                 System.out.println(musicId);
                 mediaPlayer = MediaPlayer.create(MainActivity.this,musicId);
                 mediaPlayer.start();
                 skMusic.setMax(mediaPlayer.getDuration());
                 handler.post(run);
            }
        });
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_start:
                if (mediaPlayer == null) {
                    mediaPlayer = MediaPlayer.create(this,R.raw.arms);
                    skMusic.setMax(mediaPlayer.getDuration());
                }
                mediaPlayer.start();
                handler.post(run);
                break;
            case R.id.btn_pause:
                if (mediaPlayer != null)
                    mediaPlayer.pause();
                break;
            case R.id.btn_next:
                stopMusic();
                if(currentMusic==4){
                    currentMusic = 0;
                }else {
                    currentMusic++;
                }
                startMusic(currentMusic);
                break;
            case R.id.btn_previous:
                stopMusic();
                if(currentMusic==0){
                    currentMusic =4;
                }else {
                    currentMusic--;
                }
                startMusic(currentMusic);
                break;
        }

    }
    private void initEvent() {
        btn_start.setOnClickListener(this);
        btn_pause.setOnClickListener(this);
        btn_previous.setOnClickListener(this);
        btn_next.setOnClickListener(this);
        skMusic.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                handler.removeCallbacks(run);
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mediaPlayer.seekTo(seekBar.getProgress());
                handler.postDelayed(run,100);
            }
        });
    }
    Runnable run = new Runnable() {
        @Override
        public void run() {
            skMusic.setProgress(mediaPlayer.getCurrentPosition());
            tvProgress.setText(milliSecondsToTimer(mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition()));
            handler.postDelayed(run, 100);
        }
    };



    private void initView(){
        listView = findViewById(R.id.list_music);
        tvProgress = findViewById(R.id.tv_progress);
        skMusic= findViewById(R.id.sk_music);
        btn_start = findViewById(R.id.btn_start);
        btn_pause = findViewById(R.id.btn_pause);
        btn_previous = findViewById(R.id.btn_previous);
        btn_next = findViewById(R.id.btn_next);
    }

    private void initData(){

     for (int i = 0;i<musicId.length;i++){
         MusicInfo musicInfo = new MusicInfo();
         musicInfo.setId(musicId[i]);
         musicInfo.setName(" ");
         musicInfos.add(musicInfo);
     }
    }
    public void startMusic(int currentMusic){
        mediaPlayer = MediaPlayer.create(this, musicId[currentMusic]);
        skMusic.setMax(mediaPlayer.getDuration());
        mediaPlayer.start();
        handler.post(run);
    }
    public void stopMusic(){
        if(mediaPlayer!=null){
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }
    public static String milliSecondsToTimer(long milliseconds) {
        String finalTimerString = "";
        String secondsString = "";

        //Convert total duration into time
        int hours = (int) (milliseconds / (1000 * 60 * 60));
        int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
        int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
        // Add hours if there
        if (hours == 0) {
            //finalTimerString = hours + ":";
        }
        // Pre appending 0 to seconds if it is one digit
        if (seconds < 10) {
            secondsString = "0" + seconds;
        } else {
            secondsString = "" + seconds;
        }

        finalTimerString = finalTimerString + minutes + ":" + secondsString;

        // return timer string
        return finalTimerString;
    }


}
